package com.wbsxch.shiro.service.impl;

import com.github.pagehelper.PageHelper;
import com.wbsxch.shiro.domain.User;
import com.wbsxch.shiro.dto.UserDTO;
import com.wbsxch.shiro.mapper.UserMapper;
import com.wbsxch.shiro.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int insert(UserDTO record) {
        User user = new User();
        BeanUtils.copyProperties(record,user);
        return userMapper.insert(user);
    }

    @Override
    public int delete(Long id) {
        return userMapper.delete(id);
    }

    @Override
    public int update(UserDTO record) {
        User user = new User();
        BeanUtils.copyProperties(record,user);
        return userMapper.update(user);
    }

    @Override
    public List<UserDTO> selectAll(UserDTO record) {
        PageHelper.startPage(1,10);
        User user = new User();
        BeanUtils.copyProperties(record,user);
        List<User> users = userMapper.selectAll(user);
        List<UserDTO> userDTOs = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(users)){
            userDTOs = users.stream().map(e->{
                UserDTO userDTO = new UserDTO();
                BeanUtils.copyProperties(e,userDTO);
                return userDTO;
            }).filter(e-> Objects.nonNull(e)).collect(Collectors.toList());
        }
        return userDTOs;
    }

    @Override
    public UserDTO selectOne(Long id) {
        User user = userMapper.selectOne(id);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user,userDTO);
        return userDTO;
    }

    @Override
    public UserDTO selectUserByUsername(String username) {
        User user = userMapper.selectUserByUsername(username);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user,userDTO);
        return userDTO;
    }

    @Override
    public Set<String> selectUserRoleSet(Long id) {
        return userMapper.selectUserRoleSet(id);
    }

    @Override
    public Set<String> selectUserPermissionSet(Long id) {
        return userMapper.selectUserPermissionSet(id);
    }

    @Override
    public List<UserDTO> selectAll(UserDTO userDTO, Integer currentPage, Integer pageSize) {
        if(currentPage==null){
            currentPage = 1;
        }
        if(pageSize==null){
            pageSize = 10;
        }
        PageHelper.startPage(currentPage,pageSize);
        return userMapper.selectAllUserDTO(userDTO);
    }

    @Override
    public List<UserDTO> selectAll(UserDTO userDTO, Integer currentPage, Integer pageSize, String orderBy) {
        return null;
    }
}
